/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.database.engine;

import java.io.*;
import java.sql.SQLException;

public class Clob implements java.sql.Clob {

    private char[] buf;

    public static Clob valueOf(java.sql.Clob clob) throws IOException, SQLException {
        if(clob==null) return null;
        return new Clob(clob);
    }

    public Clob(java.sql.Clob clob) throws SQLException, IOException {
        Reader reader = clob.getCharacterStream();
        CharArrayWriter caw = new CharArrayWriter();
        char[] buf = new char[1024];
        int rd;
        while ((rd = reader.read(buf)) != -1) {
            caw.write(buf, 0, rd);
        }
        reader.close();
        this.buf = caw.toCharArray();
    }

    public long length() throws SQLException {
        return buf.length;
    }

    public String getSubString(long pos, int length) throws SQLException {
        return new String(buf, 0, length);
    }

    public Reader getCharacterStream() throws SQLException {
        return new CharArrayReader(buf);
    }

    public InputStream getAsciiStream() throws SQLException {
        return new ByteArrayInputStream(new String(buf).getBytes());
    }

    public long position(String searchstr, long start) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public long position(java.sql.Clob searchstr, long start) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int setString(long pos, String str) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int setString(long pos, String str, int offset, int len) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public OutputStream setAsciiStream(long pos) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public Writer setCharacterStream(long pos) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public void truncate(long len) throws SQLException {
        if (len < buf.length) {
            char[] nb = new char[(int) len];
            System.arraycopy(buf, 0, nb, 0, (int) len);
            buf = nb;
        }
    }

    public void free() throws SQLException {
        buf = null;
    }

    public Reader getCharacterStream(long pos, long length) throws SQLException {
        return new CharArrayReader(buf, (int)pos, (int) length);
    }
}
